Bmob.initialize("141d3c4c418aa624978d74a82375788b", "1764d1a85f20e649cb4bde39fdd70ebb");

//创建类和对象实例
var people = Bmob.Object.extend("people");


//添加数据方法
function add(){
	var name=$("#name").val(),count=$("#count").val(),msg=$("#msg").val();
	if(name=='' || count==''){
		alert('您需要填写姓名和来宾人数后才可提交哦')
	}else{
		var person = new people();	
		person.save({
			name:name,
			count:count,
			msg:msg
		},{
			success:function(per){
				alert('添加成功，id为：'+per.id)
			},
			error:function(per,errData){
				console.log(errData.message)
			}
		})
	}
	
	
}

//查看数据方法
function selectAll(){
	var query=new Bmob.Query(people);
	query.limit(1000);
	var datas={
		length:'',
		items:[],
		isComeNum:0
	};
	query.find({
		success:function(result){
			datas.length=result.length;
			for(var i=0;i<result.length;i++){				
				datas.items.push(result[i])
				datas.isComeNum+=Number(result[i].get('count'));
			}
			pushDataToDom(datas);			
		},
		error:function(per,errData){
			alert("查询失败: " + errData.code + " " + errData.message)
		}
	})
}

//渲染数据到dom
function pushDataToDom(data){
	$("#length").text(data.length);
	$("#isComeNum").text(data.isComeNum);
	oldLength=data.length;
	var table=$("#result");
	for(var i=0;i<data.items.length;i++){
		var id=data.items[i].id,
		    name=data.items[i].attributes.name,
		    count=data.items[i].attributes.count,
		    msg=data.items[i].attributes.msg,
		    time=data.items[i].createdAt;
		var str="<tr> <td>"+id+"</td><td>"
			               +name+"</td><td>"
			               +count+"</td><td>"
			               +msg+"</td><td>"
			               +time+"</td> </tr>";
		table.append(str);
	}
	
	
	
}
